package com.cuiweiyou.librarybase;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;

import java.io.Serializable;

import butterknife.ButterKnife;
import butterknife.Unbinder;


/**
 * Created by macpro on 2018/5/3.
 */

public class BaseActivity extends AppCompatActivity {
    public static final String INTENT_ARG_KEY = "intent_arg";
    public static final int CODE_DEFAULT_RESULT = 12321;
    private Unbinder unbinder;
    InputMethodManager inputMethodManager;

    @Override
    protected void onResume() {
        super.onResume();
        Log.i("ard", "onResume：" + this.getLocalClassName());
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.i("ard", "onPause：" + this.getLocalClassName());
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.i("ard", "onStop：" + this.getLocalClassName());
    }

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
        unbinder = ButterKnife.bind(this);
        setWindowStatusBarColor(Color.parseColor("#69a590"));
        inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
    }

    public void delayNextActivityAndFinishCurrent(final Class clazz, long delay) {
        new Handler(getMainLooper()).postDelayed(new Runnable() {
            @Override
            public void run() {
                nextActivityKeepCurrent(clazz);
                finish();
            }
        }, delay);
    }

    public void nextActivityAndFinishCurrent(Class clazz) {
        delayNextActivityAndFinishCurrent(clazz, 0);
    }

    public void nextActivityAndFinishCurrent(Class clazz, Serializable arg) {
        nextActivityKeepCurrent(clazz, arg);
        finish();
    }

    public void nextActivityKeepCurrent(Class clazz) {
        nextActivityKeepCurrent(clazz, null);
    }

    public void nextActivityKeepCurrent(Class clazz, Serializable arg) {
        Intent i = new Intent(this, clazz);
        if (null != arg) {
            Log.e("ard", "跳转参数：" + arg.toString());
            i.putExtra(INTENT_ARG_KEY, arg);
        }
        startActivity(i);
    }

    public void nextActivityForResultKeepCurrent(Class clazz, int resultCode) {
        Intent i = new Intent(this, clazz);
        startActivityForResult(i, resultCode);
    }

    public void nextActivityForResultKeepCurrent(Class clazz, int resultCode, Serializable arg) {
        Intent i = new Intent(this, clazz);
        if (null != arg) {
            i.putExtra(INTENT_ARG_KEY, arg);
        }
        startActivityForResult(i, resultCode);
    }

    public Serializable getSerializableArg() {
        Intent intent = getIntent();
        Serializable serializable = intent.getSerializableExtra(INTENT_ARG_KEY);
        if (null != serializable) {
            Log.e("ard", "界面收到的启动参数：" + serializable.toString());
        }
        return serializable;
    }

    public void setViewHeight(View v, int height) {
        ViewGroup.LayoutParams layoutParams = v.getLayoutParams();
        layoutParams.height = height;
        v.setLayoutParams(layoutParams);
    }

    public void setWindowStatusBarColor(int colorHex) {
        Window window = getWindow();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { // 6.0
            // window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); // 黑字
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE); // 白字
        }

        int color = colorHex;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { // api21，5.0
            //取消设置透明状态栏,使 ContentView 内容不再覆盖状态栏
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

            //需要设置这个 flag 才能调用 setStatusBarColor 来设置状态栏颜色
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);

            window.setStatusBarColor(color);//设置状态栏背景颜色
            window.setNavigationBarColor(color);

            return;
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { // api19，4.4
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);//透明状态栏
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);//透明导航栏

            //设置contentview为fitsSystemWindows
            ViewGroup contentView = (ViewGroup) findViewById(android.R.id.content);
            View childAt = contentView.getChildAt(0);
            if (childAt != null) {
                childAt.setFitsSystemWindows(true);
            }
            //给statusbar着色
            View view = new View(this);
            view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight(this)));
            view.setBackgroundColor(color);
            contentView.addView(view);
        }
    }

    //无论是否隐藏都拿到statusbar高度
    private int getStatusBarHeight(Context ctx) {
        //获取status_bar_height资源的ID
        int resourceId = ctx.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            //根据资源ID获取响应的尺寸值
            return ctx.getResources().getDimensionPixelSize(resourceId);
        } else {
            try {
                Class<?> clazz = Class.forName("com.android.internal.R$dimen");
                Object object = clazz.newInstance();
                int height = Integer.parseInt(clazz.getField("status_bar_height").get(object).toString());
                return ctx.getResources().getDimensionPixelSize(height);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return 40;
    }

    public void hintKeyBoard() {
        if (inputMethodManager.isActive() && getCurrentFocus() != null) {
            if (getCurrentFocus().getWindowToken() != null) {
                inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.RESULT_UNCHANGED_SHOWN);
            }
        }
    }

    public void showKeyBoard(EditText view) {
        if (inputMethodManager.isActive() && getCurrentFocus() != null) {
            if (getCurrentFocus().getWindowToken() != null) {
                inputMethodManager.showSoftInput(view, InputMethodManager.SHOW_FORCED);
            }
        }
    }
}
